// https://leetcode.cn/problems/number-of-matching-subsequences/
// Created by ade on 2022/11/17.
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

class Solution {
public:
    int numMatchingSubseq(string s, vector <string> &words) {
        vector <vector<int>> pos(26);
        for (int i = 0; i < s.size(); i++)
            pos[s[i] - 'a'].push_back(i);
        int num = s.size();
        for (auto &word:words) {
            if (word.size() > s.size()) {
                num--;
                continue;
            }
            int p = -1;
            for (char c : word) {
                auto &ps = pos[c - 'a'];
                auto iter = upper_bound(ps.begin(), ps.end(), p);
                if (iter == ps.end()){
                    num--;
                    break;
                }
                p = *iter;
            }
        }
        return num;
    }
};

int main() {
    Solution so;
    string s = "abcde";
    vector <string> ss = {"a", "bb", "acd", "ace"};
    cout << so.numMatchingSubseq(s, ss) << endl;
    return 0;
}